< previous page page_361 next page >

Page 361
Now the problem of passing a POINT structure has been simplified. All you need to do is ask what kind of Visual Basic declaration can create this stack frame. The answer is
Private Declare Function PtInRect Lib
"user32" (lpRect As RECT, _
ByVal ptx As Long, ByVal pty As Long)
As Long
0361-01.gif
Figure T7-1:
Stack frame showing a POINT structure passed by value
Remember (from Tutorial 4) that parameters are pushed onto the stack in reverse order. Because we want the actual values of the X and Y fields on the stack, we pass the X and Y fields separately by value. The RECT structure is passed by reference.
The following sample code from the Struct.vbp project (on the CD that comes with this book) demonstrates use of the PtInRect function in Visual Basic:
Private Sub cmdPtInRect_Click()
   Dim r As RECT
   Dim pt As POINTAPI
   r.Right = 100
   r.Bottom = 100
   pt.x = 50
   pt.y = 50
   Debug.Print PtInRect(r, pt.x, pt.y)
   pt.x = 150
   Debug.Print PtInRect(r, pt.x, pt.y)
End Sub
Visual Basic's inability to pass structures by value does require you to pass the fields of the structure individually, but at least the function can be called.
The good news is that the POINT and SIZE structures (which are identical except for different field names) are the only structures I know of that are passed by value to Win32 API functions.

 
< previous page page_361 next page >